//
//  MYGCloudDiskShareSheet.swift
//  MYDoctor
//
//  Created by Denny on 2024/3/13.
//

import UIKit
class MYGCloudDiskShareSheet: UIView {
    var tapClosePageCompletion:(()->Void)?
    var tapOpenCompletion:(()->Void)?
    var tapPickCodeCompletion:(()->Void)?
    var tapValidityPeriodCompletion:(()->Void)?
    var model = MYCloudDiskContentModel()
    var time = 0
    var titleModel = MYCloudDiskValidityPeriodModel() {
        didSet {
            self.titleLB.text = titleModel.title
            self.subTitleLB.text = titleModel.subTitle
        }
    }
    @IBOutlet weak var subTitleLB: UILabel!
    @IBOutlet weak var titleLB: UILabel!
    
    
    
    @IBAction func tapClose(_ sender: Any) {
        if let cc = self.tapClosePageCompletion {
            cc()
        }
    }
    
    @IBOutlet weak var actionView: UIView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.actionView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapActionView(_ :))))
    }
    
    @IBAction func tapOpenBtn(_ sender: UIButton) {
        //        let pasteboard = UIPasteboard.general
        //        pasteboard.string = "公开链接"
        //        JFPopupView.popup.toast(hit: "已复制到剪切板")
        if let cc = self.tapOpenCompletion {
            cc()
        }
    }
    @IBAction func tapPickCodeBtn(_ sender: UIButton) {
        //        let pasteboard = UIPasteboard.general
        //        pasteboard.string = "提取码链接"
        //        JFPopupView.popup.toast(hit: "已复制到剪切板")
        if let cc = self.tapPickCodeCompletion {
            cc()
        }
    }
    
    func shareAlert(codeModel:MYGCloudShareCodeModel) {
        let textToShare:String = "医路通"
        let imageToShare:UIImage = UIImage(named: "AppIcon")!
        let token = MYGUserManager.getUser()?.verification_token ?? ""
        let pid = self.model.id
        let type = codeModel.type
        let code = codeModel.code
        //http://h5-dev.medataplus.com/#/pages/netdisk/Extract?pid=&type=&code=&isShare=1
        let urlToShare:URL = URL(string: "\(shareUrl)/#/pages/netdisk/Extract?pid=\(pid)&type=\(type)&code=\(code)&isShare=1&token=\(token)")!
        print("url\(urlToShare.absoluteString)")
        let activityItems:Array = [textToShare, imageToShare, urlToShare] as [Any]
        let activityVC:UIActivityViewController = UIActivityViewController.init(activityItems: activityItems, applicationActivities: nil)
        activityVC.excludedActivityTypes = [UIActivity.ActivityType.postToFacebook,UIActivity.ActivityType.airDrop]
        
        self.rootResponsder().navigationController?.present(activityVC, animated: true)
//        if let cc = self.tapClosePageCompletion {
//            cc()
//        }
    }
    @IBAction func tapWechat(_ sender: UIButton) {
        let type = self.model.type == 0 ? "1" :"2" //1文件夹 2：文件
        var custom_time = ""
        if time == 2 {
            custom_time = MYGHelp.after1Month()
        }
        MYGNetwork<MYGMeAPI, MYGCloudShareCodeModel>.request(target: .shareCode(param: [
            "time":"\(time + 1)",
            "f_id":self.model.id,
            "custom_time":custom_time,
            "type":type])) {[weak self] item in
                self?.shareAlert(codeModel: item)
            } failed: { _, error in
                MYGLoading.hide()
                MMYGHUD.showToast(text: error)
            }
    }
    
    @objc func tapActionView(_ tap:UITapGestureRecognizer) {
        if let cc = self.tapValidityPeriodCompletion {
            cc()
        }
        
    }
}
